import { takeEvery } from "redux-saga/effects";
import { NATIVE_CLASS } from "@common/constants";
import { callNativeFunc } from "@common/saga-helper";
import {
  SAGA_UNPACK_PUSH_2_SERVER,
  SAGA_UNPACK_LOAD_DETAILS,
  SAGA_UNPACK_REMOVE,
  SAGA_UNPACK_SAVE
} from "./saga-types";
import {
  SET_UNPACK_DETAILS
} from "./action-types";

function* pushUnpack2ServerSaga() {
  yield takeEvery(SAGA_UNPACK_PUSH_2_SERVER, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.UnpackHandler,
            action: "push2Server",
            needNonce: true,
            params: action.payload,
          },
          callback: action.payload.callback
        },
      ],
    });
  });
}

function* loadUnpackDetailsSaga() {
  yield takeEvery(SAGA_UNPACK_LOAD_DETAILS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.UnpackDetailsHandler,
            action: "loadDetails",
            needNonce: true,
            params: action.payload,
          },
          respActions: [SET_UNPACK_DETAILS],
        },
      ],
    });
  });
}

function* removeUnpackSaga() {
  yield takeEvery(SAGA_UNPACK_REMOVE, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.UnpackDetailsHandler,
            action: "remove",
            needNonce: false,
            params: action.payload,
          },
          callback: action.payload.callback
        },
      ],
    });
  });
}

function* saveUnpackSaga() {
  yield takeEvery(SAGA_UNPACK_SAVE, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.UnpackOpsHandler,
            action: "save",
            needNonce: false,
            params: action.payload,
          },
          callback: action.payload.callback
        },
      ],
    });
  });
}

export default [
  pushUnpack2ServerSaga(),
  loadUnpackDetailsSaga(),
  removeUnpackSaga(),
  saveUnpackSaga()
];